using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace LohnefinkTower
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        KeyboardState teclado_atual, teclado_anterior;
        MouseState mouse_atual, mouse_anterior;
        GamePadState joystick_atual, joystick_anterior;

        Torre torre;
        Torre torre2;
        
        bool tocandoMusica;
        Song musica;
        SoundEffect efeitoSonoro;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            IsMouseVisible = true;
            Window.Title = "Tower Defense XNA";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            //graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            efeitoSonoro = Content.Load<SoundEffect>("WindowsError");

            torre = new Torre(Content.Load<Texture2D>("circle"), Window, efeitoSonoro);
            torre2 = new Torre(Content.Load<Texture2D>("circle"), Window, efeitoSonoro);

            torre2.Posicao = new Vector2(150, 150);
            torre2.jogador = 2;

            musica = Content.Load<Song>("Kalimba");

            MediaPlayer.Play(musica);
            MediaPlayer.Pause();

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Window.Title = mouse_atual.X.ToString() + "-" + mouse_atual.Y.ToString();

            // TODO: Add your update logic here
            teclado_atual = Keyboard.GetState();
            mouse_atual = Mouse.GetState();
            joystick_atual = GamePad.GetState(PlayerIndex.One);

            torre.Update(gameTime, teclado_atual, teclado_anterior, mouse_atual, mouse_anterior);
            
            torre2.Update(gameTime, teclado_atual, teclado_anterior, mouse_atual, mouse_anterior);

            if (torre2.Bateu(torre))
            {
                torre2.visivel = false;
            }

            if (teclado_atual.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (teclado_atual.IsKeyDown(Keys.F11))
            {
                graphics.ToggleFullScreen();
            }



            if (teclado_atual.IsKeyDown(Keys.P)
                && teclado_anterior.IsKeyUp(Keys.P))
            {
                if (!tocandoMusica)
                {
                    MediaPlayer.Resume();
                    tocandoMusica = true;
                }
                else
                {
                    MediaPlayer.Pause();
                    tocandoMusica = false;
                }

            }

            if (teclado_atual.IsKeyDown(Keys.S))
            {
                MediaPlayer.Stop();
                tocandoMusica = false;
            }
            if (tocandoMusica)
            {
                if ((teclado_atual.IsKeyDown(Keys.PageUp)) && (teclado_anterior.IsKeyUp(Keys.PageUp)))
                {
                    MediaPlayer.Volume += 0.1f;
                }
                if ((teclado_atual.IsKeyDown(Keys.PageDown)) && (teclado_anterior.IsKeyUp(Keys.PageDown)))
                {
                    MediaPlayer.Volume -= 0.1f;
                }
                if ((teclado_atual.IsKeyDown(Keys.M)) && (teclado_anterior.IsKeyUp(Keys.M)))
                {
                    MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
                }
            }


            teclado_anterior = teclado_atual;
            mouse_anterior = mouse_atual;
            joystick_anterior = joystick_atual;



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            torre.Draw(gameTime, spriteBatch, torre.andando);
            torre2.Draw(gameTime, spriteBatch, torre.andando);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
